﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.ApplicationServices;
using System.Windows;
using System.Windows.Controls;

namespace OrganizeYourSearch
{
    class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceAppWrapper wrapper = new SingleInstanceAppWrapper();
            wrapper.Run(args);
        }
    }

    public class SingleInstanceAppWrapper : WindowsFormsApplicationBase
    {
        //Create a WPF app Class
        WPFApplication myWpfApp;
        public SingleInstanceAppWrapper()
        {
            this.IsSingleInstance = true;
        }
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            myWpfApp = new WPFApplication();
            myWpfApp.Run();
            return false;
        }
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
            if (eventArgs.CommandLine.Count > 0)
            {
                try
                {
                    string[] str = eventArgs.CommandLine.ToArray<string>();
                    MainWindow win = myWpfApp.MainWindow as MainWindow;
                    if (win != null)
                    {
                        win.Method_AddNewTab(str);
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Exception from OnStartupNextInstance:\n" + ex.Message);
                }
            }
        }

    }
    public class WPFApplication:Application
    {
        
            private static TagsDataProvider database;
            public static TagsDataProvider Database
            {
                get { return database; }
                set { database = value; }
            }
              
            public WPFApplication()
            {
                try{
                        Database = new TagsDataProvider();
                   }
                catch(Exception ex)
                {
                    MessageBox.Show("From WPFApplication Constructor\n"+ex.Message);
                }

            }
            protected override void OnStartup(System.Windows.StartupEventArgs e)
            {
                base.OnStartup(e);

                Application.Current.Resources.Source =
                                 new Uri("pack://application:,,,/Fluent;Component/Themes/Office2010/Silver.xaml", 
                                     UriKind.RelativeOrAbsolute);

                MainWindow mainWin = new MainWindow();
                mainWin.Show();
                this.MainWindow = mainWin;
            }
       }

}
